<template>
  <div class="product-con">
    <el-card class="box-card">
      <div class="clearfix" slot="header">
        <span><i class="iconfont icon-sousuo-copy"> </i> 筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          size="small"
          @click="onSubmit"
          >查询</el-button
        >
        <el-button
          style="float: right"
          type="button"
          size="small"
          @click="onReset"
          >重置</el-button
        >
      </div>
      <div>
        <el-form
          ref="form"
          :inline="true"
          :model="listQuery"
          label-width="80px"
        >
          <el-form-item label="商品名称:">
            <el-input v-model="listQuery.name"></el-input>
          </el-form-item>
          <el-form-item label="货号:">
            <el-input v-model="listQuery.product_sn"></el-input>
          </el-form-item>
          <el-form-item label="审核状态:">
            <el-select
              v-model="listQuery.verify_status"
              placeholder="审核状态"
              class="input-w"
            >
              <el-option label="未审核" value="0"></el-option>
              <el-option label="已审核" value="1"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="商品分类:">
            <el-cascader
              v-model="listQuery.product_category_id"
              :options="categoryOptions"
              @change="handleChange"
            ></el-cascader
          ></el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="box-card">
      <div class="clearfix">
        <span><i class="iconfont icon-dingdan1"> </i>商品数据列表</span>
        <el-button style="float: right" type="primary" size="small"
          >添加</el-button
        >
      </div>
    </el-card>
    <!-- 使用表格显示数据  :data 表格想要显示的数据-->
    <el-table :data="productList" border style="width: 100%" class="listtable">
      <el-table-column prop="id" label="编号" width="180"> </el-table-column>
      <el-table-column label="商品图片" width="180">
        <template slot-scope="scope"><img :src="scope.row.pic" /> </template>
      </el-table-column>

      <el-table-column prop="name" label="商品名称" width="180">
      </el-table-column>
      <el-table-column label="价格/货号" width="180">
        <template slot-scope="scope">
          <div>价格：{{ scope.row.price }}</div>
          <div>货号：{{ scope.row.product_sn }}</div>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" width="180">
        <template slot-scope="scope">
          {{ scope.row.verify_status | verify }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="updateProduct(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="delProduct(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  fetchProductList,
  fetchBrand,
  fetchCategory,
  delProduct,
} from "@/api/product.js";

export default {
  name: "product",
  data: function () {
    return {
      productList: [],
      listQuery: {
        pageSize: 10,
        pageNum: 1,
        name: null,
        product_sn: null,
        verify_status: null,
        brand_id: null,
        product_category_id: null,
      },
      total: 0,
      brandOptions: [{ value: "zhinan", label: "指南" }],
      categoryOptions: [
        {
          value: "zhinan",
          label: "指南",
          children: [
            {
              value: "shejiyuanze",
              label: "设计原则",
            },
          ],
        },
      ],
    };
  },

  created() {
    this.getProductList();
    this.getBrand();
    this.getCategory();
  },
  filters: {
    verify: function (value) {
      return value ? "已审核" : "未审核";
    },
  },
  methods: {
    updateProduct: function (id) {
      console.log(id);
      this.$router.push("/pms/productUpdate/" + id);
    },
    delProduct: function (id) {
      console.log(id);

      this.$confirm("此操作将永久删除该条商品数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delProduct({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
                duration: 2000,
              });

              this.getProductList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
    getProductList() {
      fetchProductList(this.listQuery).then((res) => {
        console.log(res);
        this.productList = res.product;
        this.total = res.total;
      });
    },

    getBrand() {
      fetchBrand().then((res) => {
        console.log(res);
        this.brandOptions = res.data;
      });
    },

    getCategory() {
      fetchCategory().then((res) => {
        console.log(res);
        var data = res.data;

        data.forEach((item) => {
          let children = [];

          item.children.forEach((i) => {
            children.push({ value: i.id, label: i.name });
          });
          this.categoryOptions.push({
            value: item.id,
            label: item.name,
            children: children,
          });
        });
        console.log(this.categoryOptions);
      });
    },

    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;

      this.getProductList();
    },

    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getProductList();
    },

    onSubmit() {
      console.log(this.listQuery);

      this.getProductList();
    },

    onReset() {
      this.listQuery = {
        pageSize: 10,
        pageNum: 1,
        name: null,
        product_sn: null,
      };

      this.getProductList();
    },

    handleChange(value) {
      if (value !== null && value.length === 2) {
        this.listQuery.product_category_id = value[1];
      }
    },
  },
};
</script>

<style scoped lang="scss">
.product-con {
  padding: 10px;
  margin-bottom: 100px;
  .input-w {
    width: 100px;
  }
  .listtable {
    margin-top: 10px;

    img {
      width: 50px;
    }
  }
  .box-card {
    margin-bottom: 10px;
  }
}
</style>